﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class AddProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Them du lieu vao cat 1
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", "0");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate1.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate1.Items.Add(lt);
                }
                Cate1.SelectedIndex = 0;


            }

        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            Cate2.Items.Clear();
            Cate3.Items.Clear();
        
            int index = Cate1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate2.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate2.Items.Add(lt);
                }
          
            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            Cate3.Items.Clear();
            int index = Cate2.SelectedIndex;

            int id = Convert.ToInt32(Cate2.SelectedValue);
            // Load du lieu
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
            ListItem lt = new ListItem("Chon danh muc", "-1");
            Cate3.Items.Add(lt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                Cate3.Items.Add(lt);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
      
            string filePathBigIamge = null;
            string cate1_name = Cate1.SelectedItem.ToString();

   

            int cate1_index = Cate1.SelectedIndex;

            int cate2_index = Cate2.SelectedIndex;
            int cate3_index = Cate3.SelectedIndex;

            if (cate1_index > 0)
            {
                int cate1_id = Convert.ToInt32(Cate1.SelectedValue.ToString());

                int cate2_id = 0;
                int cate3_id = 0;

            
                if (cate2_index < 1)
                {
                    cate2_id = cate1_id;
                }
                else
                {
                    cate2_id = Convert.ToInt32(Cate2.SelectedValue.ToString());
                }
                if (cate3_index < 1)
                {
                    cate3_id = cate2_id;
                }
                else { cate3_id = Convert.ToInt32(Cate3.SelectedValue.ToString()); }

                if (LIB.Validate.CheckFileType(fulBigImage.FileName))
                {
                    string id = DAL.DCollector.DBCollector.Instance.CallSQL1Param("select  IDBox_ID from tbl_IDBox where tbl_IDBox.IDBox_Active=1 LIMIT 1", true).Rows[0]["IDBox_ID"].ToString();
                    LIB.Validate saveimage = new LIB.Validate();
                    filePathBigIamge = "/Resources/Images/Image-Products/" + id + "a" + Path.GetExtension(fulBigImage.FileName) + "";
                    saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Image-Products/"), id + "a" + Path.GetExtension(fulBigImage.FileName), 500, fulBigImage.PostedFile.InputStream);
                   


                    string productname = txtNameProduct.Text;
                    string title = txtTittle.Text;
                    string summary = CKEditorMota.Text;
                    string description = CKEditorChitiet.Text;
           
                    DateTime datetime = DateTime.Now;

                    string values = "" + cate1_id + "," + cate2_id + "," + cate3_id + ",N'" + productname + "',N'" + filePathBigIamge + "',N'" + title + "',N'" + summary + "',N'" + description + "','" + datetime + "'";
                    
                    DAL.DCollector.DBCollector.Instance.StoredInsert("tbl_ThietKeWebsite",values);

                    Response.Write("<script>alert('Thêm sản phẩm thành công ...')</script>");
                }
                else
                {
                    lblCheckBigImage.Text = "Chỉ có thể upload file(.jpg,.png,.jpeg)";
                }
            }
            else
            {
                Response.Write("<script>alert('Bạn vui lòng chọn danh mục sản phẩm...')</script>");
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtNameProduct.Text = "";
            CKEditorMota.Text = "";
            CKEditorChitiet.Text = "";
         
        }
    }
}
